package cn.iocoder.yudao.module.mini.service.healthy;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mini.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.mini.dal.mysql.user.MiniUserMapper;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.iocoder.yudao.module.mini.controller.admin.healthy.vo.*;
import cn.iocoder.yudao.module.mini.dal.dataobject.healthy.HealthyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.mini.convert.healthy.HealthyConvert;
import cn.iocoder.yudao.module.mini.dal.mysql.healthy.HealthyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mini.enums.ErrorCodeConstants.*;

/**
 * 技师健康档案 Service 实现类
 *
 * @author dzy
 */
@Service
@Validated
public class HealthyServiceImpl implements HealthyService {

    @Resource
    private HealthyMapper healthyMapper;

    @Resource
    private MiniUserMapper miniUserMapper;

    @Override
    public Long createHealthy(HealthyCreateReqVO createReqVO) {
        // 插入
        HealthyDO healthy = HealthyConvert.INSTANCE.convert(createReqVO);
        healthyMapper.insert(healthy);
        // 返回
        return healthy.getId();
    }

    @Override
    public void updateHealthy(HealthyUpdateReqVO updateReqVO) {
        // 校验存在
        validateHealthyExists(updateReqVO.getId());
        // 更新
        HealthyDO updateObj = HealthyConvert.INSTANCE.convert(updateReqVO);
        healthyMapper.updateById(updateObj);
    }

    @Override
    public void deleteHealthy(Long id) {
        // 校验存在
        validateHealthyExists(id);
        // 删除
        healthyMapper.deleteById(id);
    }

    private void validateHealthyExists(Long id) {
        if (healthyMapper.selectById(id) == null) {
            throw exception(HEALTHY_NOT_EXISTS);
        }
    }

    @Override
    public HealthyDO getHealthy(Long id) {
        HealthyDO healthyDO = healthyMapper.selectById(id);
        healthyDO.setNickname(miniUserMapper.selectById(healthyDO.getMemberId()).getNickname());
        return healthyDO;
    }

    @Override
    public List<HealthyDO> getHealthyList(Collection<Long> ids) {
        return healthyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HealthyDO> getHealthyPage(HealthyPageReqVO pageReqVO) {
        if (StrUtil.isNotBlank(pageReqVO.getNickname())) {
            List<UserDO> userDOS = miniUserMapper.selectList(new LambdaQueryWrapperX<UserDO>()
                    .select(UserDO::getId)
                    .in(UserDO::getNickname, pageReqVO.getNickname()));
            List<Long> memberIds = userDOS.stream().map(UserDO::getId).collect(Collectors.toList());
            pageReqVO.setMemberIds(memberIds);
        }

        PageResult<HealthyDO> healthyDOPageResult = healthyMapper.selectPage(pageReqVO);
        List<HealthyDO> list = healthyDOPageResult.getList();
        list.forEach(item -> {
            UserDO userDO = miniUserMapper.selectById(item.getMemberId());
            item.setNickname(Objects.nonNull(userDO)?userDO.getNickname():null);
        });
        return healthyDOPageResult;
    }

    @Override
    public List<HealthyDO> getHealthyList(HealthyExportReqVO exportReqVO) {
        return healthyMapper.selectList(exportReqVO);
    }

}
